package creatures;

import java.awt.Color;
import java.awt.event.MouseEvent;
import java.io.PrintStream;
import java.util.Scanner;
import engine.Game;
import engine.map.Tile;

/**
 * The Player
 * 
 * @author Kyle_Solo
 */
public class Player extends Creature {

	/**
	 * Default constructor for the Player
	 */
	public Player() {
		character = "@";
		color = Color.white;
		speed = 5;
	}

	/**
	 * @param x
	 *            the Player's starting x-coordinate in cells
	 * @param y
	 *            the Monster's starting y-coordinate in cells
	 */
	public Player(int x, int y) {
		this();
		this.x = x;
		this.y = y;
	}

	@Override
	public String getSaveFilename() {
		return toString();
	}

	@Override
	public boolean load(Scanner data) {
		super.load(data);
		x = data.nextInt();
		y = data.nextInt();
		return true;
	}

	/**
	 * This method is called when a mouse event is triggered on the player's turn while not in any
	 * menus
	 */
	private void mouseMainGUI(MouseEvent e) {
		int x = e.getX();
		int y = e.getY();
		if (Game.map.isVisible(x, y)) {
			Creature c = Game.map.getCreature(e.getX(), e.getY());
			if (c != null) {
				Game.messenger.sendMessage(c.toString());
			} else {
				Tile t = Game.map.getTile(e.getX(), e.getY());
				if (t != null) {
					Game.messenger.sendMessage(t.toString());
				}
			}
		}
	}

	@Override
	public boolean save(PrintStream data) {
		super.save(data);
		data.append(x + " ");
		data.append(y + "");
		return true;
	}

	@Override
	public int update() {
		// the player's turn
		Game.map.updateSight(x, y, 10);
		Game.map.render(Game.display, 0, 0, 0, 0, 199, 59);
		Game.display.render();
		// todo: better input/GUI
		GUI.processInput(this);
		// clear the message buffer after the player's turn
		Game.messenger.sendMessage("");
		return 10 / speed;
	}
}
